<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Userlog extends Stourweb_Controller{

    /*
     * 日志总控制器
     *
     */
    public function before()
    {
        parent::before();
        $this->assign('parentkey',$this->params['parentkey']);
        $this->assign('itemid',$this->params['itemid']);

    }

    public function action_index()
    {
        $action=$this->params['action'];
        if(empty($action))  //显示列表
        {
            $this->display('stourtravel/tools/userlog_list');
        }
        else if($action=='read')    //读取列表
        {
            $start=Arr::get($_GET,'start');
            $limit=Arr::get($_GET,'limit');
            $keyword=Arr::get($_GET,'keyword');

            $sql = "SELECT * FROM sline_product_price_log WHERE id>0 ";
            $total =  DB::query(1, $sql)->execute()->count();
            $sql.= "ORDER BY addtime DESC ";
            $sql.= "LIMIT {$start},{$limit}";
            $list = DB::query(1, $sql)->execute()->as_array();

            $admin = DB::select()->from('admin')->execute()->as_array();
            $admin_list = array_combine(array_column($admin,'id'),array_column($admin,'username'));
            $supplier = DB::select()->from('supplier')->execute()->as_array();
            $supplier_list = array_combine(array_column($supplier,'id'),array_column($supplier,'suppliername'));
            $app = DB::select()->from('model')->execute()->as_array();
            $app_list = array_combine(array_column($app,'id'),array_column($app,'modulename'));

            foreach ($list as &$v){
                $params = json_decode($v['info'],true);
                $v['app_name'] = $app_list[$params['type_id']];
                $v['name'] = $v['admin_id']?'管理员：'.$admin_list[$v['admin_id']]:'供应商：'.$supplier_list[$v['supplier_id']];
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $v['content'] = self::return_suit_name($params).' '.$v['describe'];
            }

            $result['total']   = $total;
            $result['lists']   = $list;
            $result['success'] = true;

            echo json_encode($result);
        }
        else if($action=='save')   //保存字段
        {

        }
        else if($action=='delete') //删除某个记录
        {
            $rawdata=file_get_contents('php://input');
            $data=json_decode($rawdata);
            $id=$data->id;

            if(is_numeric($id)) //
            {
				 DB::delete('product_price_log')->where('id','=',$id)->execute();
            }
        }
        else if($action=='update')//更新某个字段
        {
            $id=Arr::get($_POST,'id');
            $field=Arr::get($_POST,'field');
            $val=Arr::get($_POST,'val');

            if(is_numeric($id))
            {
                $model=ORM::factory('product_price_log')->where('id','=',$id)->find();

            }

            if($model->id)
            {
                $model->$field=$val;

                $model->update();
                if($model->saved())
                    echo 'ok';
                else
                    echo 'no';
            }
        }
    }

    public function return_suit_name($params)
    {
        $table = St_Functions::table_field($params['type_id']);
        $suit = DB::select()->from($table[0])->where("id={$params['suit_id']}")->execute()->current();

        return '套餐名称【'.$suit[$table[2]].'】';
    }


}